package com.neusoft.neumooc.dao;

import java.util.List;
import java.util.Map;
import java.util.HashMap;

import org.springframework.stereotype.Repository;

import com.neusoft.core.common.BaseDAO;
import com.neusoft.core.common.exception.DAOException;
import com.neusoft.neumooc.vo.ContentTypeVO;

@Repository
public class ContentTypeDAOImpl extends BaseDAO implements ContentTypeDAO {

    private static final String SQL_GET_ALL_CONTENT_TYPES = 
        "SELECT typeId as typeId, " +
               "typeName as typeName, " +
               "typeDesc as typeDesc " +
        "FROM cw_contenttype";
        
    private static final String SQL_GET_CONTENT_TYPE_BY_ID = 
        "SELECT typeId as typeId, " +
               "typeName as typeName, " +
               "typeDesc as typeDesc " +
        "FROM cw_contenttype WHERE typeId = :typeId";
        
    private static final String SQL_ADD_CONTENT_TYPE = 
        "INSERT INTO cw_contenttype(typeId, typeName, typeDesc) " +
        "VALUES (:typeId, :typeName, :typeDesc)";
        
    private static final String SQL_UPDATE_CONTENT_TYPE = 
        "UPDATE cw_contenttype SET typeName = :typeName, typeDesc = :typeDesc " +
        "WHERE typeId = :typeId";
        
    private static final String SQL_DELETE_CONTENT_TYPE = 
        "DELETE FROM cw_contenttype WHERE typeId = :typeId";

    @Override
    public List<ContentTypeVO> getAllContentTypes() throws DAOException {
        try {
            List<ContentTypeVO> types = this.queryForBeanList(SQL_GET_ALL_CONTENT_TYPES, ContentTypeVO.class);
            log.debug("查询到的内容类型列表: " + types);
            return types;
        } catch (Exception e) {
            log.error("查询内容类型列表失败", e);
            throw new DAOException("查询内容类型列表失败", e);
        }
    }

    @Override
    public ContentTypeVO getContentTypeById(String typeId) throws DAOException {
        try {
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("typeId", typeId);
            log.info("================" + typeId);
            ContentTypeVO type = this.queryForBeanByBeanMap(SQL_GET_CONTENT_TYPE_BY_ID, ContentTypeVO.class, params);
            log.debug("查询到的内容类型: " + type);
            return type;
        } catch (Exception e) {
            log.error("查询内容类型失败", e);
            throw new DAOException("查询内容类型失败", e);
        }
    }

    @Override
    public boolean addContentType(ContentTypeVO contentType) throws DAOException {
        try {
            this.update(SQL_ADD_CONTENT_TYPE, contentType);
            return true;
        } catch (Exception e) {
            log.error("添加内容类型失败", e);
            return false;
        }
    }

    @Override
    public boolean updateContentType(ContentTypeVO contentType) throws DAOException {
        try {
            this.update(SQL_UPDATE_CONTENT_TYPE, contentType);
            return true;
        } catch (Exception e) {
            log.error("更新内容类型失败", e);
            return false;
        }
    }

    @Override
    public boolean deleteContentType(String typeId) throws DAOException {
        try {
            ContentTypeVO param = new ContentTypeVO();
            param.setTypeId(typeId);
            this.update(SQL_DELETE_CONTENT_TYPE, param);
            return true;
        } catch (Exception e) {
            log.error("删除内容类型失败", e);
            return false;
        }
    }
} 